<?php
class Item extends AppModel
{
	var $name = 'Item';
	var $belongsTo = 'Survey';
	var $hasMany = array('Choice'=>array('dependent'=>true), 'Rectangle'=>array('dependent'=>true));
	
	function last_item($survey_id){
		return $this->find('first', array('conditions'=>array('survey_id'=>$survey_id), 'order'=>array('Item.id DESC')));
	}
	
	function process($new_item, $choices, $image){
		$new_item['Item']['next'] = '-1';
		
		$image_tag = "";
		if(!empty($image['name'])){
			$dir = "../../files/".$new_item['Item']['survey_id'];
			exec("mkdir $dir");
			exec("mv $image[tmp_name] $dir/$image[name]");
			$image_tag = "<img src='".$new_item['Item']['survey_id']."/".$image['name']."'";
			if($new_item['Item']['returns']==4)
				$image_tag .= " id='magicPic' onclick='window.Surveyor.setTouchResponse(event.clientX, event.clientY, document.getElementById(\"magicPic\").width, document.getElementById(\"magicPic\").height)'";
				$image_tag .= " style='max-width: 100%; max-height: 100%; postition: absolute; top: 0px; left: 0px;'";
			$image_tag .= " /><br />";
		}
		
		switch($new_item['Item']['returns']){
			case 0:
				$prompt = $this->mcReturnPrompt($new_item['Item']['prompt'], $image_tag, $choices);
				break;
			case 1:
				$prompt = $this->textReturnPrompt($new_item['Item']['prompt'], $image_tag);
				break;
			case 2:
				$prompt = $this->audioReturnPrompt($new_item['Item']['prompt'], $image_tag);
				break;
			case 3: //gps
				$prompt = "";
				break;
			case 4: 
				$prompt = $this->touchScreenReturnPrompt($new_item['Item']['prompt'], $image_tag);
				break;
		}
		$new_item['Item']['prompt'] = $prompt;
		return $new_item;
	}
	
	function textReturnPrompt($p, $image_tag){
		$prompt = "<html><body>" . str_replace(array("\r\n","\n","\r"), "<br />", $p) . "<br />";
		$prompt .= $image_tag;
		$prompt .= "<input type='text' id='ans' />";
		$prompt .= "<input type='button' value='Next' onclick='window.Surveyor.setResponse(document.getElementById(\"ans\").value)' />";
		$prompt .= "<script language='javascript'>document.getElementById(\"ans\").focus();</script>";
		$prompt .= "</body></html>";
		return $prompt;
	}
	
	function mcReturnPrompt($p, $image_tag, $choices){
		$prompt = "<html><body>" . str_replace(array("\r\n","\n","\r"), "<br />", $p) . "<br />";
		$prompt .= $image_tag;
		foreach($choices as $index=>$text){
			$new_item['Choice'][$index]['value'] = $index;
			$new_item['Choice'][$index]['text'] = $text;
			$prompt .= "<input type='button' value='$text' onclick='window.Surveyor.setResponse(\"$index\")' /><br />";
		}
		$prompt .= "</body></html>";
		return $prompt;
	}
	
	function touchScreenReturnPrompt($p, $image_tag){
		$prompt = "<html><body>";
		$prompt .= $image_tag;
		$prompt .= str_replace(array("\r\n","\n","\r"), "<br />", $p);
		$prompt .= "</body></html>";
		return $prompt;
	}
	
	function audioReturnPrompt($p, $image_tag){
		$prompt = "<html><body>" . str_replace(array("\r\n","\n","\r"), "<br />", $p) . "<br />";
		$prompt .= $image_tag;
		$prompt .= "<input type='button' value='Record' onclick='window.Surveyor.mediaButton(0)' /><br />";
		$prompt .= "<input type='button' value='Stop' onclick='window.Surveyor.mediaButton(1)' /><br />";
		$prompt .= "<input type='button' value='Play' onclick='window.Surveyor.mediaButton(2)' /><br />";
		$prompt .= "<input type='button' value='Next' onclick='window.Surveyor.setResponse(\"file\")' />";
		$prompt .= "</body></html>";
		return $prompt;
	}
	
	function get_item_numbers($num_col, $result)
	{
		$item_numbers = array();
		
		for($i=0; $i<$num_col; $i++)
		{
			$field = mysql_fetch_field($result);
			
			$i_num = $this->get_item_id($field->name);
			
			$item_numbers[$i] = $i_num;
		}
		
		return $item_numbers;
	}
	
	function get_item_titles($num_col, $item_numbers)
	{
		$item_titles = array();
		
		//$table = 'items';
		
		for($i=0; $i<$num_col; $i++)
		{
			if (is_numeric($item_numbers[$i]))
			{				
				$query = mysql_query("SELECT title FROM items WHERE id = {$item_numbers[$i]}");
				$title = mysql_result($query, 0);
				$item_titles[$i] = $title;
			}
		}
		
		return $item_titles;
	}
	
	function get_fields($num_rows, $num_col, $item_numbers, $result)
	{
		$fields = array();
		
		for($i = 0; $i < $num_rows; $i++)
		{
			$row = mysql_fetch_row($result);
			
			for($j = 0; $j < $num_col; $j++)
			{
					if (is_numeric($row[$j]))
					{
						if ($j == 0)
						{
							$fields[$i][$j] = $row[$j];
						}
						else
						{
							$q = "SELECT text FROM choices WHERE item_id = {$item_numbers[$j]} AND value = {$row[$j]}";
							$text_query = mysql_query("SELECT text FROM choices WHERE item_id = ". $item_numbers[$j] ." AND value = ". $row[$j]) or die($q . mysql_error());
							$num_results = mysql_num_rows($text_query);
							if ($num_results > 0)
							{
								$m_text = mysql_result($text_query, 0);
								$fields[$i][$j] = $m_text;
								
							}
							else
							{
								$fields[$i][$j] = $row[$j];
							}
						}
					}
					else
					{
						if ($j >1)
						{
							$t = "SELECT returns FROM items WHERE item_id = {$item_numbers[$j]}";
							$returns_query = mysql_query("SELECT returns FROM items WHERE id = {$item_numbers[$j]}") or die($j." ".$t . mysql_error());
							$returns_num = mysql_result($returns_query, 0);
							$fields[$i][$j] = $returns_num;						
							
							if ($returns_num == 2)
							{
								$u = "SELECT survey_id FROM items WHERE item_id = ". $item_numbers[$j];
								$id_query = mysql_query("SELECT survey_id FROM items WHERE id = ". $item_numbers[$j]) or die ($u . mysql_error());
								$survey_id = mysql_result($id_query, 0);
								$link = "<a href='/f/$survey_id/1/".$row[$j]."'>".$row[$j]."</a>";
								$fields[$i][$j] = $link;
							}
							else
							{
								$fields[$i][$j] = $row[$j];
							}
						}
						else
						{
							$fields[$i][$j] = $row[$j];
						}
						
						
					}
			}
		}
		return $fields;
	}
	
	// FUNCTION TO PARSE A STRING WITHIN STRING
	function get_substring($string, $start, $end)
	{
		$string = " ".$string;
		$initial = strpos($string, $start);
	
		if ($initial == 0)
			return "";
	
		$initial = $initial + strlen($start);
	
		$length = strpos($string, $end, $initial) - $initial;
	
		$string_between = substr($string, $initial, $length);
 
		return $string_between;
	}

	// FUNCTION TO USE GET_SUBSTRING FUNCTION TO RETREIVE ITEM ID
	function get_item_id($string)
	{
		$num = $this->get_substring($string, "q", "_");

		return $num;
	}
}

?>
